package com.huashi.BinaryTree;


/**
 * 二叉树镜像翻转
 */
public class MirrorTree {
    public TreeNode mirrorTree(TreeNode root) {
        if (root == null)
            return null;

        TreeNode copy = new TreeNode(root.val);
        copy(root,copy);
        return copy;
    }

    public void copy(TreeNode node,TreeNode copy){
        if (node == null )
            return ;
        if (node.right!=null)
            copy.left = new TreeNode(node.right.val);
        if (node.left!=null)
            copy.right = new TreeNode(node.left.val);
        copy(node.left,copy.left);
        copy(node.right,copy.right);
    }


    //官方：
    public TreeNode mirrorTreeT(TreeNode root) {

        //终止条件
        if (root == null)
            return null;

        //翻转
        TreeNode left = mirrorTreeT(root.left);
        TreeNode right = mirrorTreeT(root.right);
        root.right = left;
        root.left = right;

        return root;
    }
}
